package Hander.Add;

import View.Add.AddProjectView;
import View.LeaderView;
import entity.Project;
import service.PersonnelService;
import service.PersonnelServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddProjectViewHandler implements ActionListener {
    private AddProjectView addProjectView;
    private LeaderView leaderView;

    public AddProjectViewHandler(AddProjectView addProjectView, LeaderView leaderView) {
        this.addProjectView = addProjectView;
        this.leaderView = leaderView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("添加".equals(text)) {
            PersonnelService projectService = new PersonnelServiceImpl();
            Project project = addProjectView.buildProject();
            Boolean addResult = projectService.add(project.getAllAttributes(), addProjectView.getTableName());
            if (addResult) {
                leaderView.reloadTable(addProjectView.getTableName());
                JOptionPane.showMessageDialog(addProjectView, "添加成功");
                addProjectView.dispose();
            } else {
                System.out.println("添加错误！");
                JOptionPane.showMessageDialog(addProjectView, "添加失败");
            }
        }
    }
}
